//
//  StateListViewController.m
//  RestaurantX
//
//  Created by Nguyen Thanh Hai on 7/19/12.
//  Copyright (c) 2012 _eBars GbR_. All rights reserved.
//

#import "StateListViewController.h"
#import "CitiesViewController.h"
#import "DSActivityView.h"

@interface StateListViewController ()

@end

@implementation StateListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"loading..."];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0f green:91.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    self.title = @"Bundesländer";
    StateParser *parser = [[[StateParser alloc] init] autorelease];
    [parser getStateWithDelegate:self];
    stateList = [[NSMutableArray alloc] init];    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
    [stateTable release];
    [stateList release];
    [super dealloc];
}

#pragma mark -
#pragma mark StateList Delegate

- (void) didEndStateParser:(NSArray*)resultDict success:(BOOL) yn{    
    if (yn) {
        [stateList setArray:resultDict];
//        NSLog(@"didEndStateParser resultDict %@",stateList);
    }
    [stateTable reloadData];
    [DSBezelActivityView removeViewAnimated:YES];
}

#pragma mark - UITableView Delegate & Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"[stateList count] %d",[stateList count]);
    return [stateList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    NSDictionary *dict = [stateList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [stateList objectAtIndex:indexPath.row];
    CitiesViewController *controller = [[[CitiesViewController alloc] init] autorelease];
    controller.title =  @"Städte";//[dict objectForKey:@"name"];
    [self.navigationController pushViewController:controller animated:YES];    
    [controller updateListCities:[dict objectForKey:@"id"]];
//    [controller updateListCities:@"2"];
}

@end
